angular.module('meConvertedMsgControllers', [])

  .controller('ConvertedMsgCtrl', function ($scope,$state,$stateParams,ConvertedMsg,Storage,Toast,Loading,ENV) {

    var memberId;

    $scope.Info = {
      ppc_name : "",
      ppc_gender : "",
      ppc_phone : "",
      ppc_address : "",
      psp_id : 0,
      ppc_state : "未审核",
      member_id : 0
    };

    //初始化商品编号
    //$scope.Info.psp_id = $stateParams.productId;

    //控制消息框的显示
    $scope.isShow = false;


    // 获取用户的信息（姓名、性别、联系方式、收货地址）
    $scope.$on('$ionicView.beforeEnter', function () {
      memberId = Storage.get("member").member_id;
      ConvertedMsg.getMemberInfo(memberId);
    });

    $scope.$on('Me.getMemberInfoForConsignee',function(){
      var result = ConvertedMsg.getMemberInfoJson();
      $scope.Info.ppc_name = result.member_name;
      $scope.Info.ppc_gender = result.member_gender;
      $scope.Info.ppc_phone = result.member_contact;
      $scope.Info.ppc_address = result.member_address;
      $scope.Info.member_id = memberId;
      $scope.Info.psp_id = ConvertedMsg.getProductId();
    });

    $scope.gotoAddConsignee = function (){
      console.log($scope.Info);
      //验证输入信息完整性
      if($scope.Info.ppc_name == ""){
        Toast.showMessage("姓名不能为空");
        /*$ionicLoading.show({
          noBackdrop: true,
          template: "姓名不能为空",
          duration:1500
        });*/
      }
      else if($scope.Info.ppc_gender == ""){
        Toast.showMessage("性别不能为空");
        /*$ionicLoading.show({
          noBackdrop: true,
          template: "性别不能为空",
          duration:1500
        });*/
      }
      else if($scope.Info.ppc_phone == ""){
        Toast.showMessage("联系电话不能为空");
        /*$ionicLoading.show({
          noBackdrop: true,
          template: "联系电话不能为空",
          duration:1500
        });*/
      }
      else if($scope.Info.ppc_address == ""){
        Toast.showMessage("收货地址不能为空");
       /* $ionicLoading.show({
          noBackdrop: true,
          template: "收货地址不能为空",
          duration:1500
        });*/
      }else{
        //新增兑换信息，并且扣除相应积分
        ConvertedMsg.addConsignee($scope.Info);
      }
    };

    $scope.$on('Me.addConsigneeResult', function () {
      var result = ConvertedMsg.getAddConsigneeResult();
      if(result.infoStatus){
        //插入成功的时候显示
        $scope.cleanInput();
        $scope.isShow = true;
      }else{
        //失败的时候显示
        Toast.showMessage(result.infoMsg);
       /* $ionicLoading.show({
          noBackdrop: true,
          template: result.infoMsg,
          duration:1500
        });*/
      }
    });

    $scope.closeShow = function () {
      $scope.isShow = false;
      $state.go('integrat-list');
    };

    //清空输入框内容
    $scope.cleanInput = function () {
      $scope.Info.ppc_name = "";
      $scope.Info.ppc_gender = "";
      $scope.Info.ppc_address = "";
      $scope.Info.ppc_phone = "";
    };


  });
